<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Cate;
use Hash;
use Session;
use DB;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 读取所有分类信息
        $cates =Cate::select(DB::raw('id,name,pid,path,concat(path,",",id) as paths'))->orderBy('paths')->get();

        // 处理cates变量,获取层级
        foreach ($cates as $key => $value) {
             $len = count(explode(",", $value->path));
             $value->name = str_repeat(' |=== ',$len-1).$value->name;
         } 
        return view('admin.cate.index',['cates'=>$cates]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 读取分类信息
        $cates = Cate::get();
        return view('admin.cate.create',['cates'=>$cates]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //表单验证

        //实例化模型对象
        $cate = new Cate;
        $cate->name = $request->name;
        $cate->pid = $request->pid;
        
        // 检测是否为一级分类
        if ($cate->pid==0) {
            $cate->pid='0';
        } else {
            // 获取父级分类
            $parent = Cate::find($cate->pid);
            $cate->path = $parent->path.','.$parent->id;
        }
        


        if($cate->save()){
            return redirect('/cate');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cates = Cate::get();
        // 读取分类信息
        $cate = Cate::findOrFail($id);

        // 显示模板
        return view('admin.cate.edit', ['cate'=>$cate,'cates'=>$cates]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //实例化模型对象
        $cate = Cate::findOrFail($id);
        $cate->name = $request->name;
        $cate->pid = $request->pid;
        //j检测是否为顶级分类
        if($cate->pid == 0) {
            $cate->path = '0';
        }else{
            //获取父级分类 pid = 1
            $parent = Cate::find($cate->pid);
            $cate->path = $parent->path .','.$parent->id;
        }

        if($cate->save()) {
            return redirect('/cate')->with('success','更新成功');
        }else{
            return back()->with('error','更新失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //获取数据
        $cate = Cate::findOrFail($id);
        //删除子分类
        //拼接path
        $path = $cate->path.','.$cate->id;
        //根据path删除子分类
        Cate::where('path','like', $path.'%')->delete();
        if($cate->delete()){
            return back()->with('success','执行成功');
        }else{
            return back()->with('error','执行失败');
        }
    }
}
